package com.ajhlp.spring_modules.web.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ajhlp.spring_modules.db.model.PageBean;
import com.ajhlp.spring_modules.db.model.Role;
import com.ajhlp.spring_modules.db.service.IRoleService;

/**
 * 角色管理
 * @author ajhlp
 *
 */
@Controller
public class RoleManageController {
	
	/**
	 * 默认分页大小
	 */
	private static int page_num = 10;
	
	private IRoleService roleService;
	
	public IRoleService getRoleService() {
		return roleService;
	}



	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}


	/**
	 * 分页查询角色
	 * @param num 页码
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET, value="/m/role/page/{num}")
	@ResponseBody
	public Map<String,Object> findRole(@PathVariable String num,String start, String limit, String iDisplayStart, String iDisplayLength){
		Map<String,Object> map = new HashMap<String, Object>();
		if(start == null){
			start = iDisplayStart;
		}
		if(limit == null){
			limit = iDisplayLength;
		}
		try {
			map.put("roles", roleService.findRoleByPage(this.getPageBean(num, start, limit)));
			map.put("total", roleService.countRole());
			map.put("iTotalRecords", map.get("total"));
			map.put("iTotalDisplayRecords", map.get("total"));
			map.put("success", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("success", false);
		}
		return map;
	}
	
	/**
	 * 获取查询分页对象
	 * 如果起始值与每页记录数不合法，则按默认分页数加页码组成分页对象，如页码也不合法，默认为第一页
	 * @param num 页码
	 * @param start 起始值
	 * @param limit 每页记录数
	 * @return
	 */
	 
	private PageBean getPageBean(String num,String start,String limit){
		
		PageBean pageBean = new PageBean();
		
		try {
			pageBean.setStart(Integer.parseInt(start));
			pageBean.setLimit(Integer.parseInt(limit));
		} catch (NumberFormatException e1) {
			// TODO Auto-generated catch block
			pageBean = this.getPageBean(num);
		}
		
		return pageBean;
	}
	
	/**
	 * 根据页码生成分页对象
	 * @param num 页码
	 * @return
	 */
	private PageBean getPageBean(String num){
		int tmp = 0;
		try {
			tmp = Integer.parseInt(num)-1;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
		}
		if(tmp < 0){
			tmp = 0;
		}
		PageBean pageBean = new PageBean();
		pageBean.setStart(tmp*page_num);
		pageBean.setLimit(page_num);
		return pageBean;
	}
	
	@RequestMapping(method=RequestMethod.GET, value="/m/role/{id}")
	@ResponseBody
	public Role findRoleById(@PathVariable String id){
		Map<String,Object> condition = new HashMap<String, Object>();
		condition.put("id", id);
		List<Role> list = this.roleService.findRole(condition);
		if(list!=null && list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	/**
	 * 新建角色
	 * @param role
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST, value="/m/role")
	@ResponseBody
	public Map<String,Object> addRole(Role role){
		Map<String,Object> map = new HashMap<String, Object>();
		try {
			roleService.addRole(role);
			map.put("success", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("success", false);
			map.put("msg", e.getMessage());
		}
		return map;
	}
	
	/**
	 * 更新角色
	 * @param role
	 * @return
	 */
	@RequestMapping(method=RequestMethod.PUT, value="/m/role/{id}")
	@ResponseBody
	public Map<String,Object> updateRole(Role role){
		Map<String,Object> map = new HashMap<String, Object>();
		try {
			roleService.updateRole(role);
			map.put("success", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("success", false);
			map.put("msg", e.getMessage());
		}
		return map;
	}
	
	/**
	 * 删除角色
	 * @param id 角色ID
	 * @return
	 */
	@RequestMapping(method=RequestMethod.DELETE, value="/m/role/{id}")
	@ResponseBody
	public Map<String,Object> deleteRole(@PathVariable String id){
		Map<String,Object> map = new HashMap<String, Object>();
		try {
			int role_id = Integer.parseInt(id);
			roleService.deleteRole(role_id);
			map.put("success", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("success", false);
			map.put("msg", e.getMessage());
		}
		return map;
	}

}
